#include "seat_wrapper.h"
#include "wlroots-full.hpp"
#include "server.h"

SeatWrapper::SeatWrapper(Server *server)
    : m_server(server)
{
    m_seat = wlr_seat_create(m_server->m_display, "seat0");

    m_request_cursor.connect(&m_seat->events.request_set_cursor, [=](void* data) {
        struct wlr_seat_pointer_request_set_cursor_event *event = (struct wlr_seat_pointer_request_set_cursor_event*)data;
        request_cursor(event);
    });

    m_request_set_selection.connect(&m_seat->events.request_set_selection, [=](void* data) {
        struct wlr_seat_request_set_selection_event *event = (struct wlr_seat_request_set_selection_event *)data;
        request_set_selection(event);
    });
}

SeatWrapper::~SeatWrapper()
{

}

void SeatWrapper::request_cursor(wlr_seat_pointer_request_set_cursor_event *event)
{
    struct wlr_seat_client *focused_client = m_seat->pointer_state.focused_client;
    /* This can be sent by any client, so we check to make sure this one is
     * actually has pointer focus first. */
    if (focused_client == event->seat_client) {
        /* Once we've vetted the client, we can tell the cursor to use the
         * provided surface as the cursor image. It will set the hardware cursor
         * on the output that it's currently on and continue to do so as the
         * cursor moves between outputs. */
        // wlr_cursor_set_surface(server->cursor, event->surface,
                               // event->hotspot_x, event->hotspot_y);
        sig_request_cursor.emit(event->surface, event->hotspot_x, event->hotspot_y);
    }
}

void SeatWrapper::request_set_selection(wlr_seat_request_set_selection_event *event)
{
    wlr_seat_set_selection(m_seat, event->source, event->serial);
}
